package org.sxp.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sxp.modules.sys.entity.SysUserEntity;
import org.sxp.modules.sys.service.SysConfigService;
import org.sxp.modules.sys.shiro.ShiroUtils;
import org.sxp.modules.workspace.entity.SpaceDO;
import org.sxp.modules.workspace.service.SpaceService;

import java.util.List;

/**
 * 系统页面视图
 *
 *
 */
@Controller
public class SysPageController {

	public static String system_sub_name_config_key = "system_sub_name";
	public static String system_name_config_key = "system_name";
	private static String[] defaultPwd = new String[]{"admin", "1qaz@WSX"};
	@Autowired
	SysConfigService configService;

	@Autowired
	SpaceService spaceService;

	@RequestMapping("modules/{module}/{url}.html")
	public String module(@PathVariable("module") String module, @PathVariable("url") String url){
		return "modules/" + module + "/" + url;
	}

	@RequestMapping(value = {"/", "index.html"})
	public String index(Model model){
		SysUserEntity userEntity = ShiroUtils.getUserEntity();
		if(userEntity == null){
			return "login";
		}
		// 强制修改密码
		boolean boo = false;
		for (String pwd : defaultPwd) {
			String stt = ShiroUtils.sha256(pwd, userEntity.getSalt());
			if(stt.equals(userEntity.getPassword())){
				boo = true;
				break;
			}
		}
		model.addAttribute("needUpdatePwd", boo);

		String name = configService.getValue(system_name_config_key);
		String subName = configService.getValue(system_sub_name_config_key);
		List<SpaceDO> spaceDOList = spaceService.list();
		model.addAttribute("systemName", name);
		model.addAttribute("spaceDOList", spaceDOList);
		model.addAttribute("systemSubName", StrUtil.isNotBlank(subName) ? StringEscapeUtils.unescapeHtml(subName) : "控制台");
		return "index1";
	}

	@RequestMapping("index1.html")
	public String index1(){
		return "index1";
	}

	@RequestMapping("login.html")
	public String login(){
		return "login";
	}

	@RequestMapping("main.html")
	public String main(){
		return "main";
	}

	@RequestMapping("404.html")
	public String notFound(){
		return "404";
	}

}
